package com.zidian.teacher.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zidian.teacher.R;
import com.zidian.teacher.bean.ListGroupBean;

import java.util.List;


/**
 * Created by Administrator on 2016/9/6 0006.
 */
public class ListGroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ListGroupBean> groupBeanList;

    public ListGroupAdapter(Context context, List<ListGroupBean> groupBeanList) {
        this.context = context;
        this.groupBeanList = groupBeanList;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_study_list_group, parent,
                false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).total.setText("(" + groupBeanList.get(position).getGroupTotal() + "人)");
            ((ItemViewHolder) holder).groupName.setText(groupBeanList.get(position).getGroupName());

            if (groupBeanList.get(position).isSelect()) {
                ((ItemViewHolder) holder).item.setBackgroundColor(Color.parseColor("#ffffff"));
            } else {
                ((ItemViewHolder) holder).item.setBackgroundColor(Color.parseColor("#eeeeee"));
            }

            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }


        }


    }

    @Override
    public int getItemCount() {
        return groupBeanList.size() == 0 ? 0 : groupBeanList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView total;
        TextView groupName;
        LinearLayout item;


        public ItemViewHolder(View view) {
            super(view);
            total = (TextView) view.findViewById(R.id.list_group_total);
            groupName = (TextView) view.findViewById(R.id.list_group_name);
            item = (LinearLayout) view.findViewById(R.id.list_group_item);
        }
    }


}
